$(function () {
    $("body").bind("mousedown", function (event) {
        if ($(event.target).parents(".ztreeDiv").length < 1 && $(event.target).parents(".menuContent").length < 1) {
            ZtreeCommon.hideMenu();
        }
    });
});
var ZtreeCommon = {
    //多选框
    checkBoxSetting: {
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        view: {
            dblClickExpand: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "nodeId",
                pIdKey: "pNodeId"
            }
        },
        callback: {
            onCheck: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    checkedNodes = zTree.getCheckedNodes(),
                    names = [],
                    ids = [];
                for (var i = 0; i < checkedNodes.length; i++) {
                    if (checkedNodes[i].isUser) {
                        names.push(checkedNodes[i].name);
                        ids.push(checkedNodes[i].id);
                    }
                }
                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                textInput.val(names.join(","));
                hiddenInput.val(ids);
                hiddenInput.trigger('change');
            },
            onClick: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                zTree.checkNode(treeNode, !treeNode.checked, false);
                ZtreeCommon.checkBoxSetting.callback.onCheck(e, treeId, treeNode);
            }
        }
    },
    //所有多选
    checkBoxAllSetting: {
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "s", "N": "s"}
        },
        view: {
            dblClickExpand: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "nodeId",
                pIdKey: "pNodeId"
            }
        },
        callback: {
            onCheck: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    checkedNodes = zTree.getCheckedNodes(),
                    names = [],
                    ids = [];
                for (var i = 0; i < checkedNodes.length; i++) {
                    names.push(checkedNodes[i].name);
                    ids.push(checkedNodes[i].id);
                }
                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                textInput.val(names.join(","));
                hiddenInput.val(ids);
                hiddenInput.trigger('change');
            },
            onClick: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                zTree.checkNode(treeNode, !treeNode.checked, false);
                ZtreeCommon.checkBoxAllSetting.callback.onCheck(e, treeId, treeNode);
            }
        }
    },

    //user用户单选
    radioSetting: {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "all"
        },
        view: {
            dblClickExpand: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "nodeId",
                pIdKey: "pNodeId"
            }
        },
        callback: {
            onClick: function (e, treeId, treeNode) {
                if (!treeNode.isUser) {
                    return;
                }
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    nodes = zTree.getSelectedNodes(),
                    v = "";
                nodes.sort(function compare(a, b) {
                    return a.id - b.id;
                });
                for (var i = 0, l = nodes.length; i < l; i++) {
                    v += nodes[i].name + ",";
                }
                if (v.length > 0) v = v.substring(0, v.length - 1);
                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                var seletedId = hiddenInput.val();
                if (seletedId == "" || seletedId != treeNode.id) {
                    textInput.val(v);
                    hiddenInput.val(treeNode.id);
                    zTree.checkNode(treeNode, true, false);
                } else {
                    textInput.val("");
                    hiddenInput.val("");
                    zTree.checkNode(treeNode, false, false);
                }
            },
            onCheck: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    checkedNode = zTree.getCheckedNodes()[0],
                    checked = treeNode.checked

                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                if (checked) {
                    textInput.val(checkedNode.name);
                    hiddenInput.val(checkedNode.id);
                } else {
                    textInput.val("");
                    hiddenInput.val("");
                }
            }
        }
    },
    //所有的单选
    radioAllSetting: {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "all"
        },
        view: {
            dblClickExpand: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "nodeId",
                pIdKey: "pNodeId"
            }
        },
        callback: {
            onClick: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    nodes = zTree.getSelectedNodes(),
                    v = "";
                nodes.sort(function compare(a, b) {
                    return a.id - b.id;
                });
                for (var i = 0, l = nodes.length; i < l; i++) {
                    v += nodes[i].name + ",";
                }
                if (v.length > 0) v = v.substring(0, v.length - 1);
                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                var seletedId = hiddenInput.val();
                if (seletedId == "" || seletedId != treeNode.id) {
                    textInput.val(v);
                    hiddenInput.val(treeNode.id);
                    zTree.checkNode(treeNode, true, false);
                } else {
                    textInput.val("");
                    hiddenInput.val("");
                    zTree.checkNode(treeNode, false, false);
                }
            },
            onCheck: function (e, treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj(treeId),
                    checkedNode = zTree.getCheckedNodes()[0],
                    checked = treeNode.checked;

                var textInput = $("input[ztreeId='" + treeId + "'][type='text']");
                var hiddenInput = $("input[ztreeId='" + treeId + "'][type='hidden']");
                if (checked) {
                    textInput.val(checkedNode.name);
                    hiddenInput.val(checkedNode.id);
                } else {
                    textInput.val("");
                    hiddenInput.val("");
                }
            }
        }
    },
    showMenu: function (input, isTop, topPx) {
        this.hideMenu();
        var ztreeId = $(input).attr("ztreeId");
        var ztreeDiv = $("#" + ztreeId).parent();
        var pidOffset = $(input).offset();
        var css = {left: pidOffset.left + "px"};
        if (!topPx) {
            topPx = 12;
        }
        if (isTop) {
            css.top = pidOffset.top + $(input).outerHeight() - topPx + "px";
        }
        ztreeDiv.css(css).slideDown("fast");
    },
    pageShowMenu: function (input) {
        this.hideMenu();
        var ztreeId = $(input).attr("ztreeId");
        var ztreeDiv = $("#" + ztreeId).parent();
        var pidOffset = $(input).offset();
        ztreeDiv.css({
            // left: pidOffset.left + "px",
            // top: pidOffset.top + $(input).outerHeight() - 12 + "px"
        }).slideDown("fast");
    },
    hideMenu: function () {
        $(".menuContent").fadeOut("fast");
    }
}
//单选


